#include <vector>
using namespace std;

class Solution {
public:
    int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
        vector<vector<int>> adj(n);
        for (auto& edge : edges) {
            adj[edge[0]].push_back(edge[1]);
            adj[edge[1]].push_back(edge[0]);
        }
        vector<bool> visited(n, false);
        return dfs(0, adj, hasApple, visited);
    }

private:
    int dfs(int node, vector<vector<int>>& adj, vector<bool>& hasApple, vector<bool>& visited) {
        visited[node] = true;
        int time = 0;
        for (int neighbor : adj[node]) {
            if (!visited[neighbor]) {
                int subTime = dfs(neighbor, adj, hasApple, visited);
                if (subTime > 0 || hasApple[neighbor]) {
                    time += subTime + 2;
                }
            }
        }
        return time;
    }
};